﻿using System;
using System.Data;
using System.Text;

namespace WinFormClient
{
    public class DaoInterface
    {
        private string _tableName;
        private MethodCollection _methods;

        public DaoInterface(string tableName,MethodCollection col)
        {
            this._tableName = tableName;
            this._methods = col;
        }

        public void Builder()
        {
            string content
            = this.Header()
            + this.Methods()
            + this.Footer()
            ;

            Tools.TextSaveToFile(content, "Interface", "I" + this._tableName+".cs");
        }

        #region header
        protected string Header()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("#region namespace" + Environment.NewLine);
            sb.Append("using System;" + Environment.NewLine);
            sb.Append("using System.Collections.Generic;" + Environment.NewLine);
            sb.Append("using System.Text;" + Environment.NewLine);
            sb.Append("#endregion" + Environment.NewLine);
            sb.Append(Environment.NewLine);
            sb.Append("namespace " + MapXml.NameSpace + ".Interface" + Environment.NewLine);
            sb.Append("{" + Environment.NewLine);
            sb.Append("    public interface I" + this._tableName + Environment.NewLine);
            sb.Append("    {" + Environment.NewLine);
            return sb.ToString();
        }
        #endregion

        #region methods
        protected string Methods()
        {
            StringBuilder sb = new StringBuilder();
            Method mth = null;
            for (int i = 0; i < this._methods.Count;i++ )
            {
                mth = this._methods[i];
                sb.Append(Environment.NewLine);
                sb.Append("        ");
                if (mth.AccessMethod == DataAccessMethod.ExecuteQueryForList)
                {
                    sb.Append("IList<" + mth.ReturnType + ">");
                }
                else
                {
                    sb.Append(mth.ReturnType);
                }

                sb.Append(" " + mth.MethodName + "(" + ((!String.IsNullOrEmpty(mth.ParameterType)) ? (mth.ParameterType + " " + mth.ParameterName) : "") + ");" + Environment.NewLine);
                sb.Append(Environment.NewLine);
            }
            return sb.ToString();
        }
        #endregion

        #region footer
        protected string Footer()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("    }" + Environment.NewLine);
            sb.Append("}" + Environment.NewLine);
            return sb.ToString();
        }
        #endregion

        public void Dispose()
        {
            this._methods.Dispose();
        }
    }
}
